﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using forum.ApplicationCode;
using forum.Data;


namespace forum
{
    public partial class forumMaster : System.Web.UI.MasterPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
           

            


            if (Membership.IsLoggedIn())
            {

                if (Session["mySubForumList"]==null)
                {
                    initializeMySubForumSession();
                }

                divNotLoggedIn.Visible = false;
                lblHalloUser.Text = "hello " + Membership.GetUserName();
                if (!Membership.IsAdmin())
                {
                    lnkbtnAdminArea.Visible = false;
                    adminSeparator.Visible = false;
                }
                if (Membership.IsModerator())
                {
                    ddlCanBeBannedForum.DataSource = ListManager.GetUserSubForumModerator(Membership.GetUserId());
                    ddlCanBeBannedForum.DataTextField = "_Text";
                    ddlCanBeBannedForum.DataValueField = "_Value";
                    ddlCanBeBannedForum.DataBind();
                    ddlCanBeBannedForum.Items.Insert(0, new ListItem("Choose Sub forum", "0"));
                    ddlCanBeBannedForum.Items[0].Selected = true;

                    ddlCanBeBannedUsers.Items.Insert(0, new ListItem("Choose User", "0"));
                    ddlCanBeBannedUsers.Items[0].Selected = true;
                    

                }
                // not moderator
                else
                {
                    lnkbtnBannedUsers.Visible = false;
                }



                // user actibe checkbox
                using (forumDataContext data = new forumDataContext())
                 {
                   int isActive = data.sp_IsUserActive(Membership.GetUserId()).FirstOrDefault().Column1.Value;
                   if (isActive > 0)
                       chkboxActive.Checked=true;
                 }

            }
            else
            {
                Session["mySubForumList"] = null;
                Session["unreadThreads"] = null;
                divLoggedIn.Visible=false;
                chkboxActive.Checked = false;
            }

            ddlSerachByUser.DataSource = ListManager.GetUserList();
            ddlSerachByUser.DataTextField = "_Text";
            ddlSerachByUser.DataValueField = "_Value";
            ddlSerachByUser.DataBind();
            ddlSerachByUser.Items.Insert(0, new ListItem("Choose User", "0"));
            ddlSerachByUser.Items[0].Selected = true;





        }

        private void initializeMySubForumSession()
        {
            Dictionary<int, int> tmp =new Dictionary<int, int>();

            using (forumDataContext data = new forumDataContext())
            {
                var subForums = data.sp_GetSubForumList();
                foreach (var forum in subForums)
                {
                    int mySubForums = data.sp_GetSubForumsWithMyThreads(Membership.GetUserId(), forum.subForumId).FirstOrDefault().numOfThreads.Value;
                    if (mySubForums>0)
                    {
                        int lastThread = data.sp_GetLastThreadInSubForum(forum.subForumId).FirstOrDefault().threadId;
                        tmp.Add(forum.subForumId, lastThread);
                        
                    }
                }
            }
            Session["mySubForumList"] = tmp;
        }
    }
}